package com.wps.shell.controls.scroll
{
	import com.wps.shell.controls.CommonWindow;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.events.DragEvent;
	import mx.events.MoveEvent;
	import mx.managers.CursorManager;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class ScrollWindowThumb extends CommonWindow
	{
		public function ScrollWindowThumb()
		{
			super();
		}
		
		override protected function createChildren():void
		{
			setStyle("borderThicknessBottom", 0);
			setStyle("borderThicknessLeft", 5);
			setStyle("borderThicknessRight", 5);
			setStyle("borderThicknessTop", 0);
			setStyle("headerHeight", 0);
			resizeable = true;
			setStyle("skinClass", ScrollWindowThumbSkin);
			super.createChildren();
			backGround.setStyle("skinClass", ScrollWindowBackgroundSkin);
			addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, _moveHandler);
			addEventListener(MouseEvent.MOUSE_UP, _cancelDragHandler, true);
			addEventListener(MouseEvent.ROLL_OUT, _cancelDragHandler);
			systemManager.stage.addEventListener(Event.MOUSE_LEAVE, _cancelDragHandler);
			invalidateDisplayList();
		}
		
		//----------------------------------------------------------------------
		//
		//			拖拽相关
		//
		//---------------------------------------------------------------------
		private var _isDraging: Boolean;
		private var _isChangingSize: Boolean;
		private var _lastX: Number;
		private var _lastY: Number;
		private static var _oldWidth: Number;
		private static var _oldHeight: Number;
		private static var _oldRight: Number;
		private static var _oldBottom: Number;

		private function _mouseDownHandler(event: MouseEvent): void
		{
			if(cursorManager.currentCursorID == CursorManager.NO_CURSOR)			
			{
				_isDraging = true;
				startDrag(false, new Rectangle(0, 0, this.parent.width - this.width, 1));	
			}
		}
		
		private function _moveHandler(event: MouseEvent): void
		{
			if(_isDraging)
			{
				_dispatchEvent();
			}
		}
		
		private function _cancelDragHandler(event: Event): void
		{
			_isDraging = false;
			stopDrag();
		}
		
		private function _dispatchEvent(): void
		{
			var e: ScrollWindowEvent = new ScrollWindowEvent(ScrollWindowEvent.Move);
			e.scale = this.parent.width / this.width;
			e.site = this.parent.width / this.x;
			dispatchEvent(e);
		}
		
		override protected function stopResizing():void
		{
			super.stopResizing();
			_dispatchEvent();
		}
		
	}
}